package org.jsping.music;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jsping.entity.Music;
import com.alibaba.fastjson.JSON;
/**
 * 添加音乐的servlet
 */
@WebServlet("/AddMusic.html")
public class AddMusic extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private NewMusic nm = new NewMusic();	//歌曲爬虫
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String mName = request.getParameter("mname");
		mName = mName.replaceAll(" ","");
		nm.setMusic(mName);		//获取歌曲名，歌曲歌手
		nm.setMusicInfo();		//获取歌曲图片，歌曲真实地址
		nm.getMlyric();			//获取歌词
		Map<String, Music> mapMusic = nm.getMusicMap();
		
		for (String key : mapMusic.keySet()) {
			System.out.println(key+"---<"+mapMusic.get(key).getMurl());
		}
		
		PrintWriter pw = response.getWriter();
		String jsonStr = JSON.toJSONString(mapMusic);
		
		System.out.println("打印过去的字符串:"+jsonStr);
		pw.print(jsonStr);
		pw.flush();
		pw.close();
		nm.getMusicMap().clear();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
